//
//  Status.swift
//  SinaWeibo
//
//  Created by 李旭阳 on 15/11/14.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class Status: NSObject {
    
    var created_at : String?
    var id: Int = 0
    var text: String?
    var source: String?
    var user: User?
    
    //配图视图数组
    var pic_urls: [[String: String]]?
    
    var retweeted_status: Status?
    
    //MARK: 图形数组
    var imageURLs: [NSURL]? {
        guard let urls = pic_urls else{
            return nil
        }
        
        var tempArray = [NSURL]()
        for item in urls {
            let url = NSURL(string: item["thumbnail_pic"]!)
            tempArray.append(url!)
        }
        return tempArray
    }
    
    //MARK: 定义计算型属性  同时代表 原创微博和转发微博的图片数组
    var pictureURLs: [NSURL]? {
        if retweeted_status != nil{
            return retweeted_status?.imageURLs
        }
        return imageURLs
    }
    
    //MARK: 使用KVC 设置初始值
    init(dict: [String: AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    //MARK: User 转模型
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            //对user 属性初始化
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
            }
            return
        }
        
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject]{
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    //MARK: 过滤没有用到的字段
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {  }
}
